<?php ob_flush();

	if ( $_POST ) {
		global $error;
		$error = FALSE;

		global $erroremail;
		$erroremail = FALSE;
	}
	global $username;
	$username = $_SESSION['user'];
	
	global $gender;
	global $email;
	global $aboutme;
	global $achievement;
	global $commentnumber;
	global $postnumber;
	
	//konek ke db
	$con = mysql_connect("localhost","progin","progin");
	if (!$con){
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("progin_171_13509027", $con);
?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Profile</title>
	<link rel="stylesheet" type="text/css" href="css/profil.css"/>
	<link rel="stylesheet" type="text/css" title="theme1" href="css/theme1.css"/>
	<script type="text/javascript" src="profil.js"></script>
	<?php
		include 'application/pop_script.php';
	?>
</head>

<body>
	<?php
			include 'views/header_view.php';
	?>
	<div class="primaryContent">
		<div class="container">
			<div id="profileContent">
				<?php
					echo "<h2> $username </h2>"; 
					$con = mysql_connect("localhost","progin","progin");
					
					$result = mysql_query("SELECT * FROM user WHERE username='$username'");
					$row = mysql_fetch_array($result);
					$avatar = $row['avatar'];
					if($row['gender']=="L"){$gender = "Male";}else {$gender = "Female";}
					$email = $row['email'];
					$aboutme = $row['about_me'];
					$achievement = $row['achievement'];

					$result = mysql_query("SELECT * FROM `dikomentar` WHERE `username`='$username'");
					$commentnumber = mysql_num_rows($result);

					$result = mysql_query("SELECT * FROM `ngepost` WHERE `username`='$username'");
					$postnumber = mysql_num_rows($result);

				?>	
				
				<img src=<?php echo $avatar; ?> id='avatarprofil'/>
				<form name="profil-form" class="profil-form" action="profil.php" method="POST" enctype="multipart/form-data">
					<ul id='listprofil'>
						<li> <?php
								//validasi avatar
								if ( $_POST ) {
									if ($_FILES["editavatar"]["error"] <= 0){
										if ($_FILES["editavatar"]["type"] != "image/jpeg"){
											$error = TRUE;
											echo "<input type='file' name='editavatar' id='editavatar'/>";									
											echo "<i><br/>Input image must be jpg or jpeg. </i><br />";
											echo '<style type="text/css"> #editavatar{background-color:#f27b75;} </style>';
										}
									}
									if (!$error){
										echo "<input type='file' name='editavatar' id='editavatar' style='display: none;' />";									
									}
								} else {
									echo "<input type='file' name='editavatar' id='editavatar' style='display: none;' />";									
								}
							?>
							<a href='#' id='editAvatarLink' onclick='return editAvatarFunction("editavatar", "editAvatarLink", "cancelAvatarLink", "submitProfil")' <?php if($_POST){if($error) echo "style='display: none;'";} ?> >edit avatar</a>
							<a href='#' id='cancelAvatarLink' onclick='return cancelAvatarFunction("editavatar", "editAvatarLink", "cancelAvatarLink", "submitProfil")' style='display: none;' >cancel edit</a>
						</li>
						<li> <?php echo "$gender"; ?> </li>
						<li> <?php
								//validasi email
								if ( $_POST ) {
									$email = $_POST["editemail"];
									if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i", $_POST["editemail"])==FALSE) {
										$error = TRUE;
										$erroremail = TRUE;
										echo "<input type='text' name='editemail' id='editemail' value='$email' />";									
										echo "<i><br/> Not a valid email address. </i>";
										echo '<style type="text/css"> #editemail{background-color:#f27b75;} </style>';
									} else {
										$sql = "SELECT count(*) FROM `user` WHERE `email`='$email' AND `username`!='$username'";  
										$taken = mysql_result(mysql_query($sql),0);  
										if($taken){ 
											$error = TRUE;
											$erroremail = TRUE;
											echo "<input type='text' name='editemail' id='editemail' value='$email' />";									
											echo "<i> <br/>This email is already used. Please pick another on. </i><br/>"; 
											echo '<style type="text/css"> #editemail{background-color:#f27b75;} </style>';
										} 
									}
									if (!$erroremail){
										echo "<input type='text' name='editemail' id='editemail' value='$email' style='display: none;'/>";									
									}
								} else {
									echo "<input type='text' name='editemail' id='editemail' value='$email' style='display: none;'/>";									
								}
							?>	
							<span id='currentEmail'> <?php if($_POST){$email = $_POST["editemail"]; if(!$erroremail) echo "$email";} else echo "$email"; ?> </span>
							<a href='#' id='editEmailLink' onclick='return editFunction("currentEmail", "editemail", "editEmailLink", "cancelEmailLink", "submitProfil")' <?php if($_POST){if($erroremail) echo "style='display: none;'";} ?> >edit email</a>
							<a href='#' id='cancelEmailLink' onclick='return cancelFunction("currentEmail", "editemail", "editEmailLink", "cancelEmailLink", "submitProfil")' style='display: none;' >cancel edit</a>
						</li>
						<li> <span id='currentAbout'><?php if($_POST){$aboutme = $_POST["editabout"];} echo "$aboutme"; ?></span>
							<textarea name="editabout" id='editabout' rows="70" cols="200" style='display: none;'><?php echo "$aboutme"; ?> </textarea>
							<a href='#' id='editAboutLink' onclick='return editFunction("currentAbout", "editabout", "editAboutLink", "cancelAboutLink", "submitProfil")'>edit about</a>
							<a href='#' id='cancelAboutLink' onclick='return cancelFunction("currentAbout", "editabout", "editAboutLink", "cancelAboutLink", "submitProfil")' style='display: none;' >cancel edit</a>
						</li>
						<li> 
							<?php 
								if ($achievement==0)
									echo "Big Bang";
								else if ($achievement==1)
									echo "Moon Triton";
								else if ($achievement==2)
									echo "Black Dwarf";
								else if ($achievement==3)
									echo "Spaceship";
								else if ($achievement==4)
									echo "Solar";
								else if ($achievement==5)
									echo "Black Hole";
								else if ($achievement==6)
									echo "Milky Way";
								else if ($achievement==7)
									echo "Andromeda";
							?> 
						</li>
						<li> <?php echo "$commentnumber Comments"; ?></li>
						<li> <?php echo "$postnumber Posts"; ?></li>
					</ul>		
					<input type='submit' name='submitProfil' id='submitProfil' value='Save Changes' <?php if($_POST){if($error) echo "style='display: inline;'"; else echo "style='display: none;'";} else echo "style='display: none;'"; ?> /><br/>
				</form>									
				<?php
				if ( $_POST ) {
					if($error==FALSE){	
						$email = $_POST["editemail"];
						$aboutme = $_POST["editabout"];

						$query = "UPDATE `user` SET `email`='$email', `about_me`='$aboutme'
								  WHERE  `username` =  '$username';";
						
						$con = mysql_connect("localhost","progin","progin");
						if (!mysql_query($query,$con)){
							die('Error: ' . mysql_error());
						}
						mysql_close($con);
						
						if ($_FILES["editavatar"]["error"] <= 0){
							//delete avatar sebelumnya
							$filename = "avatar/$username.jpg";
							unlink($filename);
							
							//upload avatar
							$_FILES["editavatar"]["name"] = $username.".jpg";
							move_uploaded_file($_FILES["editavatar"]["tmp_name"], "avatar/" . $_FILES["editavatar"]["name"]);
						}
						header("Cache-Control: no-cache");
					}
				}
				?>
			</div>
			<div class="sorting-bar">
				<ul class="content-type">
					<li><a class="current" href="#"><?php echo "$username's "?>Posts</a></li>
				</ul>
			</div>
			<div id="content">
				<?php $i=0; while($i< $ncontent) { ?>
				<!-- post -->
				<div class="portofolio">
					<!-- content -->
					<a class="imgcontainer" href="index.php?page=konten&id=<?php echo $post[$i]['ID_post'] ?>">
						<?php if ($post[$i]['wujud']==2) { ?>
						<img class="bestcontent" height="300" width="400" src="<?php echo $post[$i]['link'] ?>">
						<?php } else if ($post[$i]['wujud']==0) { ?>
							<span class="link">
								<span class="url"> <?php echo $post[$i]['link']?> </span><br/>
								<span class="desc"> <?php echo $post[$i]['deskripsi'] ?> </span>
						</span>
							<?php } else if ($post[$i]['wujud']==1) { 
							$url = $post[$i]['link'];
							preg_match('/[\\?\\&]v=([^\\?\\&]+)/',$url,$matches);
							$id = $matches[1];
							$width = '400';
							$height = '300';
							echo '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="http://www.youtube.com/v/' . $id . '&amp;hl=en_US&amp;fs=1?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $id . '&amp;hl=en_US&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';						
						} ?>
					</a>
					
					<div class="info">
						<!-- title -->
						<br/>
						<a class="title" href="index.php?page=konten&id=<?php echo $post[$i]['ID_post'] ?>"><?php echo $post[$i]['judul'] ?></a>
						<!-- name -->
						<br/>
						<span class="name">
							<a href="?page=profile&user=username" id="name-<?php echo $post[$i]['ID_post'] ?>"><?php echo $post[$i]['username'] ?>
							</a>
						</span>

						<span id="space">_</span>
						<!-- time -->
						<span class="waktu">
							<span id="time-<?php echo $post[$i]['ID_post'] ?>"><?php echo time() - $post[$i]['time'] ?>
							</span>
						</span>
						<!-- like-dislike -->
						<br/>
						<span class="likecount">
							<img class="mark" src="theme/like-mark.png">
							<div id="likes-<?php echo $post[$i]['ID_post'] ?>"><?php echo $post[$i]['total_like'] ?></div>
						</span>
						<!-- nComment -->
						<span id="space">_</span>
						<span class="ncomment">
							<img class="mark" src="theme/comment-mark.png">
							<span id="ncomment-<?php echo $post[$i]['ID_post'] ?>" ><?php echo $post[$i]['jml_komentar'] ?></span>
						</span>
						<br/><br/>
						<button id="but_like<?php echo $post[$i]['ID_post'] ?>" class="but_like" onclick="updateLike(<?php echo $post[$i]['ID_post'] ?>);">Like</button>
						<button id="but_dislike<?php echo $post[$i]['ID_post'] ?>" class="but_dislike" onclick="updateDislike(<?php echo $post[$i]['ID_post'] ?>);">Dislike</button>
					</div>
				</div>
				<br/>
				<?php if($i<$ncontent-1) echo "<hr class='line'>"?>
			<?php $i++;} ?>
			</div>
			<div class="paginasiin">
				<ul>
					<li>Page: </li>
					<?php 
						$i = 1;
						while($i<=$npage) {
							echo "<li><span id='space'>_</span><a href='?page=profile&p=" . ($i) ."'> ". ($i) ." </a></li>";
							$i++;
						}
					?>
				</ul>
			</div>
		</div>
	</div>
</body>

</html>
<?php ob_flush(); ?>
